package com.hy.common;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Base64;

/**
 * 对简单的加解密操作进行集中封装
 * @author hy
 * @version 1.0 2019/10/9/13:10
 */
public enum CipherUtils implements Cip {
    BASE64 {
        @Override
        public String encoder(String encoderStr) {
            return Base64.getEncoder().encodeToString(encoderStr.trim().getBytes());
        }

        @Override
        public String decoder(String decoderStr) {
            return new String(Base64.getDecoder().decode(decoderStr));
        }
    },
    URL {
        @Override
        public String encoder(String encoderStr) {
            String s = "";
            try {
                s = URLEncoder.encode(encoderStr, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return s;
        }

        @Override
        public String decoder(String decoderStr) {
            String s = "";
            try {
                s = URLDecoder.decode(decoderStr, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return s;
        }
    },
//    HmacSHA256{
//        @Override
//        public String encoder(String encoderStr) {
//            Mac hmacSha256 = Mac.getInstance("HmacSHA256");
//            hmacSha256.init(new SecretKeySpec(appsecret.getBytes(), "HmacSHA256"));
//            byte[] hmacSha256Bytes = hmacSha256.doFinal(this.createBaseSignature().getBytes());
//            return null;
//        }
//
//        public String encoder(String encoderStr,String key) {
//            Mac hmacSha256 = Mac.getInstance("HmacSHA256");
//            hmacSha256.init(new SecretKeySpec(appsecret.getBytes(), "HmacSHA256"));
//            byte[] hmacSha256Bytes = hmacSha256.doFinal(this.createBaseSignature().getBytes());
//            return null;
//        }
//
//        @Override
//        public String decoder(String decoderStr) {
//
//            return null;
//        }
//    }
}

interface Cip {

    /**
     * 返回加密了的字符串
     */
    String encoder(String encoderStr);

    /**
     * 返回解密了的字符串
     */
    String decoder(String decoderStr);
}
